﻿namespace OP10.ViewModels
{
    public class ST06TestMachineManagerViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        public ST06TestMachineManagerViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            //var QG1 = new CylinderModel("ST6电阻测试上下", 0, false, false);
            var QG2 = new CylinderModel("ST6电阻测试顶升", 0, false, false);
            var QG3 = new CylinderModel("ST6电阻测试夹持", 0, false, false);

            //QGList.Add(QG1);
            QGList.Add(QG2);
            QGList.Add(QG3);


            //开启定时器任务
            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(200);
            dispatcherTimer.Tick += OnTick;
            dispatcherTimer.Start();


        }

        private void OnTick(object? sender, EventArgs e)
        {

            CurrentValue = TestMachinePLCToPC_Global.nCurrentResist;
            //气缸状态
            foreach (var item in QGList)
            {

                switch (item.Name)
                {
                    case "ST6电阻测试上下":
                        item.QG_Status = ST06TestUpDownCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST06TestUpDownCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST06TestUpDownCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;

                    case "ST6电阻测试顶升":
                        item.QG_Status = ST06TestJackCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST06TestJackCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST06TestJackCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;

                    case "ST6电阻测试夹持":
                        item.QG_Status = ST06TestClampCy.cylinderPLCToPC_Global.nCyStatus;
                        item.bOriginSensor = ST06TestClampCy.cylinderPLCToPC_Global.bOriginSensor;
                        item.bWorkSensor = ST06TestClampCy.cylinderPLCToPC_Global.bWorkSensor;
                        break;

                    default:
                        break;
                }

            }
        }


        #region 事件


        private DelegateCommand _GoBackCommand;

        public DelegateCommand GoBackCommand =>
            _GoBackCommand ?? (_GoBackCommand = new DelegateCommand(ExecuteGoBackCommand));

        void ExecuteGoBackCommand()
        {
            NavigationParameters param = new NavigationParameters();
            param.Add("StationName", "ST06");
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, AppViews.ST06View, param);
        }

        private DelegateCommand<string> _QGDownCommand;
        public DelegateCommand<string> QGDownCommand =>
            _QGDownCommand ?? (_QGDownCommand = new DelegateCommand<string>(ExecuteQGDownCommand));

        void ExecuteQGDownCommand(string parameter)
        {
            switch (parameter)
            {
                case "ST6电阻测试上下":
                    ST06TestUpDownCy.cylinderPCToPLC_Global.bManualBT = true;
                    break;
                case "ST6电阻测试顶升":
                    ST06TestJackCy.cylinderPCToPLC_Global.bManualBT = true;
                    break;
                case "ST6电阻测试夹持":
                    ST06TestClampCy.cylinderPCToPLC_Global.bManualBT = true;
                    break;
                default:
                    break;
            }
        }


        private DelegateCommand<string> _QGUpCommand;
        public DelegateCommand<string> QGUpCommand =>
            _QGUpCommand ?? (_QGUpCommand = new DelegateCommand<string>(ExecuteQGUpCommand));

        void ExecuteQGUpCommand(string parameter)
        {
            switch (parameter)
            {
                case "ST6电阻测试上下":
                    ST06TestUpDownCy.cylinderPCToPLC_Global.bManualBT = false;
                    break;
                case "ST6电阻测试顶升":
                    ST06TestJackCy.cylinderPCToPLC_Global.bManualBT = false;
                    break;
                case "ST6电阻测试夹持":
                    ST06TestClampCy.cylinderPCToPLC_Global.bManualBT = false;
                    break;
                default:
                    break;
            }
        }

        private DelegateCommand _TriggerDownCmd;
        public DelegateCommand TriggerDownCmd =>
            _TriggerDownCmd ?? (_TriggerDownCmd = new DelegateCommand(ExecuteTriggerDownCmd));

        void ExecuteTriggerDownCmd()
        {
            TestMachinePCToPLC_Global.Trigger = true;
        }

        private DelegateCommand _TriggerUpCmd;
        public DelegateCommand TriggerUpCmd =>
            _TriggerUpCmd ?? (_TriggerUpCmd = new DelegateCommand(ExecuteTriggerUpCmd));

        void ExecuteTriggerUpCmd()
        {
            TestMachinePCToPLC_Global.Trigger = false;
        }

        private DelegateCommand _ResetDownCmd;
        public DelegateCommand ResetDownCmd =>
            _ResetDownCmd ?? (_ResetDownCmd = new DelegateCommand(ExecuteResetDownCmd));

        void ExecuteResetDownCmd()
        {
            TestMachinePCToPLC_Global.RST = true;
        }

        private DelegateCommand _ResetUpCmd;
        public DelegateCommand ResetUpCmd =>
            _ResetUpCmd ?? (_ResetUpCmd = new DelegateCommand(ExecuteResetUpCmd));

        void ExecuteResetUpCmd()
        {
            TestMachinePCToPLC_Global.RST = false;

        }


        private DelegateCommand _SetModeCommand;
        public DelegateCommand SetModeCommand =>
            _SetModeCommand ?? (_SetModeCommand = new DelegateCommand(ExecuteSetModeCommand));

        void ExecuteSetModeCommand()
        {
            TestMachinePCToPLC_Global.SetMode = true;
        }

        private DelegateCommand _UnSetModeCommand;
        public DelegateCommand UnSetModeCommand =>
            _UnSetModeCommand ?? (_UnSetModeCommand = new DelegateCommand(ExecuteUnSetModeCommand));

        void ExecuteUnSetModeCommand()
        {
            TestMachinePCToPLC_Global.SetMode = false;

        }

        private DelegateCommand _OpenTestCommand;
        public DelegateCommand OpenTestCommand =>
            _OpenTestCommand ?? (_OpenTestCommand = new DelegateCommand(ExecuteOpenTestCommand));

        void ExecuteOpenTestCommand()
        {
            TestMachinePCToPLC_Global.TurnOnOutput = true;
            IsCheck02 = false;
            TestMachinePCToPLC_Global.TurnOffOutput = false;

        }

        private DelegateCommand _UnOpenTestCommand;
        public DelegateCommand UnOpenTestCommand =>
            _UnOpenTestCommand ?? (_UnOpenTestCommand = new DelegateCommand(ExecuteUnOpenTestCommand));

        void ExecuteUnOpenTestCommand()
        {
           // TestMachinePCToPLC_Global.TurnOnOutput = false;

        }

        private DelegateCommand _CloseTestCommand;
        public DelegateCommand CloseTestCommand =>
            _CloseTestCommand ?? (_CloseTestCommand = new DelegateCommand(ExecuteCloseTestCommand));

        void ExecuteCloseTestCommand()
        {
            TestMachinePCToPLC_Global.TurnOffOutput = true;
            IsCheck01 = false;
            TestMachinePCToPLC_Global.TurnOnOutput = false;
        }

        private DelegateCommand _UnCloseTestCommand;
        public DelegateCommand UnCloseTestCommand =>
            _UnCloseTestCommand ?? (_UnCloseTestCommand = new DelegateCommand(ExecuteUnCloseTestCommand));

        void ExecuteUnCloseTestCommand()
        {
           // TestMachinePCToPLC_Global.TurnOffOutput = false;
        }


        private DelegateCommand _PingBiCommand;
        public DelegateCommand PingBiCommand =>
            _PingBiCommand ?? (_PingBiCommand = new DelegateCommand(ExecutePingBiCommand));

        void ExecutePingBiCommand()
        {
            TestMachinePCToPLC_Global.TestByPassBt = true;
        }
        private DelegateCommand _UnPingBiCommand;
        public DelegateCommand UnPingBiCommand =>
            _UnPingBiCommand ?? (_UnPingBiCommand = new DelegateCommand(ExecuteUnPingBiCommand));

        void ExecuteUnPingBiCommand()
        {
            TestMachinePCToPLC_Global.TestByPassBt = false;

        }


        #endregion


        #region 属性  



        private ObservableCollection<CylinderModel> _QGList;
        public ObservableCollection<CylinderModel> QGList
        {
            get { return _QGList ?? (_QGList = new ObservableCollection<CylinderModel>()); }
            set { SetProperty(ref _QGList, value); }
        }


        private float _CurrentValue;
        public float CurrentValue
        {
            get { return _CurrentValue; }
            set { SetProperty(ref _CurrentValue, value); }
        }

        private bool _IsCheck01;
        public bool IsCheck01
        {
            get { return _IsCheck01; }
            set { SetProperty(ref _IsCheck01, value); }
        }


        private bool _IsCheck02;
        public bool IsCheck02
        {
            get { return _IsCheck02; }
            set { SetProperty(ref _IsCheck02, value); }
        }
        #endregion
    }
}
